package com.v2.activity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.widget.DatePicker;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.jakewharton.rxbinding3.view.RxView;
import com.v2.request.RequestCenter;
import com.v2.utils.TToast;
import com.v2.utils.okhttp.OkHttpRequestCallback;
import com.zou.lotterylucky.R;
import com.zou.lotterylucky.adapters.HistoryAdapter;
import com.zou.lotterylucky.adapters.RedeemAdapter;
import com.zou.lotterylucky.data.RedeemItem;
import com.zou.lotterylucky.data.WinningHistory;
import com.zou.lotterylucky.databinding.ActivitySettingInnerBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class SettingInnerActivity extends BaseActivity<ActivitySettingInnerBinding> {

    private Calendar calendar = Calendar.getInstance();
    private LinearLayoutManager linearLayoutManager;
    private List<RedeemItem> redeemList = new ArrayList<>();
    private RedeemAdapter adapter;

    private LinearLayoutManager hLinearLayoutManager;
    private HistoryAdapter hAdapter;
    private List<WinningHistory> winningHistoryList = new ArrayList<>();

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected int layoutId() {
        return R.layout.activity_setting_inner;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate() {
        mBinding.topbar.setTitle(R.string.neibushezhi);
        mBinding.topbar.addLeftBackImageButton().setOnClickListener(v -> onBackPressed());

        mBinding.startDate.setText(dateFormat.format(new Date()));
        mBinding.endDate.setText(dateFormat.format(new Date()));

        DatePickerDialog.OnDateSetListener startDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                mBinding.startDate.setText(dateFormat.format(calendar.getTime()));
            }
        };

        DatePickerDialog.OnDateSetListener endDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                mBinding.endDate.setText(dateFormat.format(calendar.getTime()));
            }
        };

        RxView.clicks(mBinding.startDate).throttleFirst(1000, TimeUnit.MILLISECONDS).subscribe(unit -> {
            new DatePickerDialog(this, startDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
        });

        RxView.clicks(mBinding.endDate).throttleFirst(1000, TimeUnit.MILLISECONDS).subscribe(unit -> {
            new DatePickerDialog(this, endDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
        });

        mBinding.searchBtn.setOnClickListener(v -> loadRedeem());

        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mBinding.redeemView.setLayoutManager(linearLayoutManager);
        //todo adapter = new RedeemAdapter(redeemList, this);
        mBinding.redeemView.setAdapter(adapter);
        loadRedeem();
    }

    private void displayDialog(String date) {
        /*RequestCenter.getWinningHistoryByDateRange(new OkHttpRequestCallback() {
            @Override
            public void onSuccess(String result) {

            }

            @Override
            public void onFailure(String error) {
                TToast.show(error);
            }
        });*/
    }

    private void loadRedeem() {
        RequestCenter.appGetOrders("", "", "", "", "", new OkHttpRequestCallback() {
            @Override
            public void onSuccess(String result) {

            }

            @Override
            public void onFailure(String error) {
                TToast.show(error);
            }
        });
    }
}
